//{include("manager.asl")}
//{include("contractor.asl")}
// Agent manager in project COMP667-Proj

//Randomly generate a value, to be replace by some calculation of utility
price(_Service,X) :- .random(R) & X = (5*R)+10.

//initial belief that "game" is the initiator, to be dynamically loaded in the future
plays(gamerole,game). 
plays(contestant1,_contestantname,_score).
plays(contestant2,_contestantname,_score).
game_state(available).
game_difficulty("Easy").
contestant_count(0).
question(_questionid,_questiontext,_correctanswernumber,_answer1,_answer2,_answer3,_answer4,_difficulty,_fieldname,_learningobjecttype).
/* Plans */

// send a message to the initiator introducing myself as a participant
+plays(gamerole,In):true
	//set the variable Me with my name, i.e. host1
   <- .my_name(Me);
   	//introduce self to the gamerole as a host
    .send(In,tell,introduction(host,Me));
    //make myself available
   // -+game_state(available);
    .print("introducing myself:", Me, " to ",In).

   
//test CNP
+!testCNP(Ontology):true
	//query and set the A variable to initiator
  <- ?plays(gamerole,A);
  	//querry the proposal and set the CNPId,Task and Offer variables
	 ?proposal(CNPId,Task,Offer);
	 //print out the CNPId
	 .print("delivering on contract:",CNPId);
	 //notifiy the initiator, testedCNP(_Ontology,_ContractId) exists in game.asl
     .send(A, tell, testedCNP(Ontology,CNPId)).
      //changed Ag to robot

     
     // answer to Call For Proposal   
@c1 +cfp(CNPId,Task)[source(A)]
   :  plays(gamerole,A) & price(Task,Offer) & game_state(available)
   <- +proposal(CNPId,Task,Offer); // remember my proposal
      .send(A,tell,host_propose(CNPId,Offer)).
	  
@c2 +cfp(CNPId,Task)[source(A)]//if the cfp didn't come from the initiator then
//refuse the cfp.
	: ~plays(gamerole,A)
	 <- .send(A,tell,host_refuse(CNPId)).
//if the host is already engaged for a game
@c3 +cfp(CNPId,Task)[source(A)]
: game_state(unavailable)
<-.send(A,tell,host_refuse(CNPId)).

 
@cr1 +accept_proposal(CNPId)
   :  proposal(CNPId,Task,Offer)
   <- .my_name(Me);
   .print(Me," my proposal '",Offer,"' won host contract ",CNPId,
             " for ",Task,"!");
             -+game_state(unavailable);
			 !Task;
        	  ?plays(gamerole,A);
			 .send(A,tell,host_completed_contract(CNPId)).
      // do the task and report to initiator
      
@cr2 +reject_proposal(CNPId)
   <- .print("I lost CNP ",CNPId, ".");
      -proposal(CNPId,_,_,_). // clear memory

 @ac1 +introduction(contestant,Contestant):contestant_count(0)
 <- .print("first contestant found");
	+plays(contestant1,Contestant,0);
	?contestant_count(Count);
 	NewCount=Count+1;
 	-+contestant_count(NewCount).
 
 @ac2 +introduction(contestant,Contestant):contestant_count(1)
 <- .print("second contestant found");
	+plays(contestant2,Contestant,0);
	?contestant_count(Count);
 	NewCount=Count+1;
 	-+contestant_count(NewCount);
 	!beginGame.
 	
@bg [atomic] +!beginGame:contestant_count(2)
<- !askquestion.

@ask [atomic] +!askquestion:contestant_count(2)
<- ?game_difficulty(Diff);
//-+question(_,_,_,_,_,_,_,_,_,_);
 ca.athabascau.quizmaster.internalactions.getquestion(Diff,Question); 
  .print("Asking Question...");
//       +game_state(question_posed);   // remember the state of the CNP
      .findall(Name,introduction(contestant,Name),LP);
      .print("Sending question to ",LP);
//    .send(LP,tell,question(Questionid,Questiontext,Answer1,Answer2,Answer3,Answer4,Difficulty,Fieldname,Learningobjecttype));
      .send(LP,tell,Question);
     +Question.//;
    // ?question(Questionid,Questiontext,Correctanswernumber,Answer1,Answer2,Answer3,Answer4,Difficulty,Fieldname,Learningobjecttype);
 // the deadline of the CNP is now + 4 seconds, so
      // the event +!contract(Id) is generated at that time
       // .at("now +5 seconds", { +!get_answer(Questionid,Correctanswernumber) }).
     
     
     +question_answer(Questionid,Answer)
   : true
   <- !get_answer(Questionid,Answer).
      
@ga1[atomic]
+!get_answser(Questionid,Correctanswernumber)
   :true  //game_state(question_posed)
   <- //-+game_state(question_answered);
      .findall(Name,question_answer(Questionid,Answer),L);
      .print("Offers are ",L).
      //L \== []; // constraint the plan execution to at least one offer
      //.min(L,offer(WOf,WAg)); // sort offers, the first is the best
      //.print("Winner is ",WAg," with ",WOf);
      //!announce_result(CNPId,L,WAg);
     //-+game_state(unavailable).
 
 -!get_answer(Questionid,Answernumber)
   <- .my_name(Me);
   .print(Me," Question: ",Questionid," has failed!").
 
 
 